﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using SmartSpace.WpfShell.ServiceRef;
using System.Windows;
using SmartSpace.WpfShell.Modules.Dynamic.Controls;
using System.Windows.Data;
using SmartSpace.Dynamic.Controls;

namespace SmartSpace.WpfShell.Modules.Dynamic.Util
{
    public static class UIBuilder
    {
        public static FrameworkElement CreateFieldControl(FieldMetadata metadata)
        {
            Binding binding = new Binding();
            //binding.Converter = new DetailDataConvertor();
            //binding.ConverterParameter = metadata;
            binding.Path = new PropertyPath("[" + metadata.Name + "]");
            binding.Mode = BindingMode.TwoWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

            if (metadata.Type == "identity")
            {
                IdentityControl fieldControl = new IdentityControl();
                fieldControl.SetBinding(IdentityControl.IdentityProperty, binding);
                (fieldControl as ISsControl).IsReadOnly = !metadata.CanModify;
                return fieldControl;
            }
            else if (metadata.Type == "date")
            {
                DateControl fieldControl = new DateControl();
                fieldControl.HorizontalAlignment = HorizontalAlignment.Left;
                fieldControl.SetBinding(DateControl.DateProperty, binding);
                (fieldControl as ISsControl).IsReadOnly = !metadata.CanModify;
                return fieldControl;
            }
            else if (metadata.Type == "time")
            {
                TimeControl fieldControl = new TimeControl();
                fieldControl.HorizontalAlignment = HorizontalAlignment.Left;
                fieldControl.SetBinding(TimeControl.TimeProperty, binding);
                (fieldControl as ISsControl).IsReadOnly = !metadata.CanModify;
                return fieldControl;
            }
            else if (metadata.Type == "textarea")
            {
                TextAreaControl fieldControl = new TextAreaControl();
                fieldControl.Height = 200;
                fieldControl.SetBinding(TextAreaControl.TextProperty, binding);
                (fieldControl as ISsControl).IsReadOnly = !metadata.CanModify;
                return fieldControl;
            }
            else
            {
                TextBoxControl fieldControl = new TextBoxControl();
                fieldControl.Width = 200;
                fieldControl.HorizontalAlignment = HorizontalAlignment.Left;
                fieldControl.SetBinding(TextBoxControl.TextProperty, binding);
                (fieldControl as ISsControl).IsReadOnly = !metadata.CanModify;
                return fieldControl;
            }
        }
    }
}
